package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bwhx.eruler.bean.OfflineOrder;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.service.OfflineOrderService;
import com.bwhx.eruler.service.RulerService;
/**
 * @author Cuilitang
 *
 */

@Controller
public class OfflineOrderController extends BaseController {
	
	@Autowired
	OfflineOrderService oOrderService;
	@Autowired
	RulerService rulerService;
	
	
	@ResponseBody
	@RequestMapping(value = "/page.OfflineOrderList.ashx")
	 public Map orderList(Integer page,Integer rows,OfflineOrder oo) {
		
		
		if(oo.getoOReceiverName()!=null){
			log.info(oo.getoOReceiverName().length()+"==============================");

		}
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,oo);
		Map map = new HashMap();
		try{
			List<OfflineOrder> list = oOrderService.list(p);
			Integer count = oOrderService.count(oo);

			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	@ResponseBody
	@RequestMapping(value = "/page.saveOOrder.ashx")
	public String saveOOrder(OfflineOrder order) {
		try{
			order.setoOTime(System.currentTimeMillis()/1000);
			
			order.setoOKuGuanId(this.getSessionStaff().getsId());
			
			oOrderService.insert(order);
		
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@RequestMapping(value = "/page.offlineorderdetails.ashx")
	 public ModelAndView OfflineOrderDetails(@RequestParam String oId){
		OfflineOrder oOrder = oOrderService.get(oId);
		
	    return new ModelAndView("/order/offlineorderdetails.jsp","oOrder",oOrder);  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.processOOrder.ashx")
	 public Map<String,String> processOrder(@RequestBody String b,HttpSession session){		
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("status", "1");
		
		OfflineOrder oOrder = new OfflineOrder();
		oOrder.setoODoneStaffID(this.getSessionStaff().getsId().toString());
		oOrder.setoODoneTime(new Date());
		List<Ruler> rulers = new ArrayList<Ruler>();
		Map<String, String> map = new HashMap<String, String>();	
		
		String[] split = b.split("&");
		String opcode="";
		String opversion="";
		for (String string : split) {
			String[] keyValue = string.split("=");
			map.put(keyValue[0], keyValue[1]);
		}
		
		Integer oOPNumber = Integer.parseInt(map.get("oOPNumber"));
		String oOBuyerId = map.get("oOBuyerId");
		/*String oOType = map.get("oOType");
		String oOReceiver = map.get("oOReceiver");*/
		

		String oOId = map.get("oOId");
		oOrder.setoOId(Integer.parseInt(oOId));
		oOrder.setoOIsDone(true);//已完成
		oOrder.setoOPNumber(oOPNumber);
		if(oOBuyerId!=null){
			oOrder.setoOBuyerId(Integer.parseInt(oOBuyerId));
		}
		
		
		
		String oPCode="";
		ArrayList<String> opcList = new ArrayList<String>();
		//code和版本列表
		for(int i=1;i<=oOPNumber;i++){
			String opc=map.get("opcode_"+i);
			if(opcList.contains(opc)){
				resultMap.put("status", "conflict");
				resultMap.put("RSN", opc);
				return resultMap;
			}else{
				opcList.add(opc);
			}
			
			
			Ruler zaiKuRuler = rulerService.findBySn(opc);
			
			if(null==zaiKuRuler){
				resultMap.put("status", "unknown");
				resultMap.put("RSN", opc);
				return resultMap;
			}
			opcode=opc+",";
			oPCode+=opcode;
			
			
			Ruler ruler = new Ruler();
			ruler.setRsn(opc);
			opversion=map.get("opversion_"+i);
			ruler.setrVersion(Integer.parseInt(opversion));
			ruler.setrSellTime(System.currentTimeMillis()/1000);
			ruler.setrState(1);
			/*ruler.setrSellerId(1);//TODO;*/
			String rBuyerId = map.get("oOBuyerId");
			if(rBuyerId!=null&&rBuyerId!=""){
				ruler.setrBuyerId(Integer.parseInt(rBuyerId));
				ruler.setrGoWhere(1);
				ruler.setrSellerId(Integer.parseInt(map.get("oOReceiverId")));
			}else{
				ruler.setrBuyerId(Integer.parseInt(map.get("oOReceiverId")));
				ruler.setrGoWhere(0);
				ruler.setrSellerId(Integer.parseInt(map.get("oOReceiverId")));
			}
			
		
			rulers.add(ruler);
			
			
		}
		
		oOrder.setoOPCodes(oPCode.substring(0, oPCode.length()-1));
		
		oOrderService.update(oOrder,rulers);
		
				
		return resultMap;  
	  }
	
	
	@ResponseBody
@RequestMapping(value = "/page.offlineorderdel.ashx")
 public Map<String,String> OfflineOrderDel(@RequestParam String oOId){
	Map<String,String> resultMap = new HashMap<String, String>();
	resultMap.put("status", "1");
	oOrderService.del(oOId);

    return resultMap;  
  }
	
	
}
